package com.example.demo.model;

import com.baidu.bas.annotation.Erupt;
import com.baidu.bas.annotation.EruptField;
import com.baidu.bas.annotation.config.QueryExpression;
import com.baidu.bas.annotation.sub_field.Edit;
import com.baidu.bas.annotation.sub_field.EditType;
import com.baidu.bas.annotation.sub_field.Searchable;
import com.baidu.bas.annotation.sub_field.View;
import com.baidu.bas.annotation.sub_field.sub_edit.Search;
import com.baidu.bas.upms.model.EruptUser;
import com.baidu.bas.upms.model.base.HyperModel;
import com.deepzero.mmma.api.enums.SmsType;
import com.deepzero.mmma.api.vo.Variable;
import com.example.demo.enums.AuditStatus;
import com.example.demo.model.custom.SmsTemplateDataProxy;
import com.vladmihalcea.hibernate.type.json.JsonStringType;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;

import javax.persistence.*;
import java.util.List;

/**
 * @author liuzhenchuan@foxmail.com
 * @date 2020/12/22
 */
@Setter
@Getter
@Erupt(name = "短信模板",orderBy = "id desc",dataProxy = {SmsTemplateDataProxy.class})
@Table(name = "ma_sms_tempalte")
@Entity
@TypeDef(name = "json", typeClass = JsonStringType.class)
@DynamicUpdate
@NoArgsConstructor
public class SmsTemplate extends HyperModel {
    /**
     * sms 类型
     */
    @Enumerated(EnumType.STRING)
    @EruptField(
            views = @View(title = "短信类型"),
            edit = @Edit(title = "短信类型",type = EditType.HIDDEN),
            searchable = @Searchable(omitBlank = true,permitOperation = {QueryExpression.in,QueryExpression.EQ})
    )
    SmsType type;
    /**
     * 名称
     */
    @EruptField(
            views = @View(title = "短信名称"),
            edit = @Edit(title = "短信名称",type = EditType.HIDDEN)
    )
    String name;

    @EruptField(
            views = @View(title = "短信标签"),
            edit = @Edit(title = "短信标签",type = EditType.HIDDEN)
    )
    String category;

    @EruptField(
            views = @View(title = "短信签名"),
            edit = @Edit(title = "短信签名",type = EditType.HIDDEN),
            searchable = @Searchable(omitBlank = true,permitOperation = {QueryExpression.in,QueryExpression.EQ})
    )
    String signature;
    /**
     * 供应商关于内容的限制
     */
    @EruptField(
            views = @View(title = "内容说明"),
            edit = @Edit(title = "内容说明",type = EditType.HIDDEN)
    )
    String tips;
    /**
     * 模板内容
     */
    @EruptField(
            views = @View(title = "模板内容"),
            edit = @Edit(title = "模板内容",type = EditType.HIDDEN)
    )
    @Lob
    String content;
    /**
     * 模板变量
     */
    @Type(type = "json")
    @Column(name = "vars", columnDefinition = "json")
    @EruptField(
            views = @View(title = "模板变量"),
            edit = @Edit(title = "模板变量",type = EditType.HIDDEN)
    )
    private List<Variable> vars;
    /**
     * 被使用的task数量
     */
    @EruptField(edit = @Edit(title = "引用任务数",readOnly = true))
    private Integer referTaskNum = 0;

    @EruptField(
            views = @View(title = "申请说明"),
            edit = @Edit(title = "申请说明",type = EditType.HIDDEN)
    )
    String remark;

    @EruptField(
            views = @View(title = "备注"),
            edit = @Edit(title = "备注",type = EditType.HIDDEN)
    )
    String info;

    @Enumerated(EnumType.STRING)
    @EruptField(
            views = @View(title = "模板状态"),
            edit = @Edit(title = "模板状态",type = EditType.HIDDEN,readOnly = true),
            searchable = @Searchable(omitBlank = true,permitOperation = {QueryExpression.in,QueryExpression.EQ})
    )
    AuditStatus status = AuditStatus.AUDINING;

    @ManyToOne
    @EruptField(
            views = {
                    @View(title = "ID", column = "id"),
                    @View(title = "账号", column = "name")
            },
            edit = @Edit(title = "账号", type = EditType.REFERENCE_TABLE, search = @Search),
            searchable = @Searchable(omitBlank = true,permitOperation = {QueryExpression.in,QueryExpression.EQ})
    )
    private Account account;

    @EruptField(
            views = @View(title = "外部模板ID"),
            edit = @Edit(title = "外部模板ID",type = EditType.HIDDEN,readOnly = true)
    )
    String outerId;

    @ManyToOne(
            fetch = FetchType.EAGER
    )
    @EruptField(
            views = @View(title = "创建人", column = "name"),
            edit = @Edit(title = "创建人", type = EditType.REFERENCE_TABLE, search = @Search,readOnly = true)
    )
    private EruptUser createUser;

    public SmsTemplate(Long id ,AuditStatus status) {
        setId(id);
        setStatus(status);
    }
}
